﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string onhand_Add(onhand model, int ACID);


        [OperationContract]
        string onhand_Update(onhand model, int ACID);


        [OperationContract]
        string DeleteonhandByMoreKey(string GoodsCode, string WarehouseID, int ACID);


        [OperationContract]
        onhand GetonhandByMoreKey(string GoodsCode, string WarehouseID, int ACID);

    }
    public partial class Service : IService
    {
        #region  添加onhand表数据
        /// <summary>
        /// 添加onhand
        /// </summary>
        /// <param name="model">onhand实体</param>
        /// <returns>空或错误</returns>
        public string onhand_Add(onhand model, int ACID)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库编号』不能为空！";
            }
            if (model.onhandcount == null)
            {
                return "『库存数』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.onhand.Count(p => p.GoodsCode == model.GoodsCode && p.WarehouseID == model.WarehouseID) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.onhand.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加onhand表数据事物用重构
        /// <summary>
        /// 添加onhand事物用重构
        /// </summary>
        /// <param name="model">onhand实体</param>
        /// <returns>空或错误</returns>
        private string onhand_Add(onhand model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库编号』不能为空！";
            }
            if (model.onhandcount == null)
            {
                return "『库存数』不能为空！";
            }
            if (ctx.onhand.Count(p => p.GoodsCode == model.GoodsCode && p.WarehouseID == model.WarehouseID) > 0)
            {
                return "主键已存在！";
            }
            ctx.onhand.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改onhand表数据
        /// <summary>
        /// 修改onhand
        /// </summary>
        /// <param name="model">onhand实体</param>
        /// <returns>空或错误</returns>
        public string onhand_Update(onhand model, int ACID)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库编号』不能为空！";
            }
            if (model.onhandcount == null)
            {
                return "『库存数』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.onhand.Count(p => p.GoodsCode == model.GoodsCode && p.WarehouseID == model.WarehouseID) == 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    onhand onhandmodel = ctx.onhand.Single(p => p.GoodsCode == model.GoodsCode && p.WarehouseID == model.WarehouseID);
                    if (!string.IsNullOrEmpty(model.GoodsCode))
                    {
                        onhandmodel.GoodsCode = model.GoodsCode;
                    }
                    if (!string.IsNullOrEmpty(model.WarehouseID))
                    {
                        onhandmodel.WarehouseID = model.WarehouseID;
                    }
                    if (model.onhandcount != null)
                    {
                        onhandmodel.onhandcount = model.onhandcount;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        /// <summary>
        /// 修改onhand
        /// </summary>
        /// <param name="model">onhand实体</param>
        /// <returns>空或错误</returns>
        public string onhand_Update(onhand model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (string.IsNullOrEmpty(model.WarehouseID))
            {
                return "『仓库编号』不能为空！";
            }
            if (model.onhandcount == null)
            {
                return "『库存数』不能为空！";
            }
            try
            {
                if (ctx.onhand.Count(p => p.GoodsCode == model.GoodsCode && p.WarehouseID == model.WarehouseID) == 1)
                {
                    return "未找到指定记录或记录不唯一！";
                }
                onhand onhandmodel = ctx.onhand.Single(p => p.GoodsCode == model.GoodsCode && p.WarehouseID == model.WarehouseID);
                if (!string.IsNullOrEmpty(model.GoodsCode))
                {
                    onhandmodel.GoodsCode = model.GoodsCode;
                }
                if (!string.IsNullOrEmpty(model.WarehouseID))
                {
                    onhandmodel.WarehouseID = model.WarehouseID;
                }
                if (model.onhandcount != null)
                {
                    onhandmodel.onhandcount = model.onhandcount;
                }
                return string.Empty;
            }
            catch (Exception ex)
            {
                return " 修改失败!错误详细信息：" + ex.Message;
            }
        }
        #endregion

        #region  删除onhand表ByGoodsCode数据
        /// <summary>
        /// 删除onhandByGoodsCode
        /// </summary>
        /// <param name="GoodsCode">产品编号</param>
        /// <param name="WarehouseID">仓库编号</param>
        /// <returns>空或错误</returns>
        public string DeleteonhandByMoreKey(string GoodsCode, string WarehouseID, int ACID)
        {
            if (string.IsNullOrEmpty(GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (string.IsNullOrEmpty(WarehouseID))
            {
                return "『仓库编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.onhand.Count(p => p.GoodsCode == GoodsCode && p.WarehouseID == WarehouseID) != 1)
                    {
                        return null;
                    }
                    onhand model = ctx.onhand.Single(p => p.GoodsCode == GoodsCode && p.WarehouseID == WarehouseID);
                    ctx.onhand.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  获取onhand表ByGoodsCode数据
        /// <summary>
        /// 获取onhandByGoodsCode
        /// </summary>
        /// <param name="GoodsCode">产品编号</param>
        /// <param name="WarehouseID">仓库编号</param>
        /// <returns>当前表</returns>
        public onhand GetonhandByMoreKey(string GoodsCode, string WarehouseID, int ACID)
        {
            if (string.IsNullOrEmpty(GoodsCode))
            {
                return null;
            }
            if (string.IsNullOrEmpty(WarehouseID))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.onhand.Count(p => p.GoodsCode == GoodsCode && p.WarehouseID == WarehouseID) != 1)
                    {
                        return null;
                    }
                    return ctx.onhand.SingleOrDefault(p => p.GoodsCode == GoodsCode && p.WarehouseID == WarehouseID);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  获取onhand表数据
        /// <summary>
        /// 获取onhand
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<onhand></returns>
        public List<onhand> onhand_Get(Expression<Func<onhand, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.onhand.ToList();
                        }
                        else
                        {
                            return ctx.onhand.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.onhand.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.onhand.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  获取onhand表数据计数
        /// <summary>
        /// 获取onhand表数据计数
        /// </summary>
        /// <returns>List<onhand></returns>
        public int onhand_GetCount(Expression<Func<onhand, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.onhand.Count();

                    }
                    else
                    {
                        return ctx.onhand.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion

        #region  获取onhand分页表数据
        /// <summary>
        /// 获取onhand分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<onhand></returns>
        public List<onhand> onhand_Getpage(int pageIndex, int pageSize, Expression<Func<onhand, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.onhand.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.onhand.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.onhand.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.onhand.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion

        #region  批量修改onhand表数据
        /// <summary>
        /// 批量修改onhand
        /// </summary>
        /// <param name="predicate">Expression<Func<onhand, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<onhand,onhand>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int onhand_batchUpdate(Expression<Func<onhand, bool>> predicate, Expression<Func<onhand, onhand>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.onhand.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion

        #region  批量修改onhand表数据事物用重构
        /// <summary>
        /// 批量修改onhand事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<onhand, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<onhand,onhand>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int onhand_batchUpdate(Expression<Func<onhand, bool>> predicate, Expression<Func<onhand, onhand>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.onhand.Update(predicate, updater);
            return ok;
        }
        #endregion

        #region  批量删除onhand表数据
        /// <summary>
        /// 批量删除onhand
        /// </summary>
        /// <param name="predicate">Expression<Func<onhand, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int onhand_batchDel(Expression<Func<onhand, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.onhand.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion

        #region  批量删除onhand表数据事物用重构
        /// <summary>
        /// 批量删除onhand事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<onhand, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int onhand_batchDel(Expression<Func<onhand, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.onhand.Delete(predicate);
            return ok;
        }
        #endregion

    }
}

